<template>
  <view class="content">
    <Navbar />
    <scroll-view
      scroll-y
      class="main"
      refresher-enabled
      :refresher-triggered="refresherTriggered"
      @refresherrefresh="refresherrefresh"
      @scrolltolower="scrolltolower"
    >
      <!-- <swiper :bannerList="bannerList" height="280rpx" /> -->
      <classA :classList="classList" />
      <!-- 热门品牌 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hotMutli" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in homeNew"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <!-- 热门品牌 -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            ></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            ></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            ></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            ></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <Guess
        :homeGoodsGuessLike="homeGoodsGuessLike"
        :homeGoodsGuessLikeTotal="homeGoodsGuessLikeTotal"
      />
    </scroll-view>
  </view>
</template>

<script>
import Navbar from './components/Navbar.vue'
import swiper from './components/swiper.vue'
import classA from './components/class.vue'
import {
  getBannerAPI,
  getClassAPI,
  getHotAPI,
  getNewAPI,
  getGuessLikeAPI,
} from '@/api/home'
import Guess from '@/components/Guess/Guess.vue'
export default {
  data() {
    return {
      bannerList: [],
      classList: [],
      hotMutli: [],
      homeNew: [],
      homeGoodsGuessLike: [],
      pages: 1,
      // 所有商品总条数
      homeGoodsGuessLikeTotal: '',
      // 下拉刷新
      refresherTriggered: false,
    }
  },
  onLoad() {
    this.loading = false
    this.pageParams = { page: 1, pageSize: 20 }
    this.getBanner()
    this.getClass()
    this.getHot()
    this.getNew()
    this.getGuessLike()
  },
  methods: {
    // 下拉刷新所以数据
    refresherrefresh() {
      this.refresherTriggered = true
      this.bannerList = []
      this.classList = []
      this.hotMutli = []
      this.homeNew = []
      this.homeGoodsGuessLike = []
      this.pages = 1
      this.homeGoodsGuessLikeTotal = ''
      this.pageParams = { page: 1, pageSize: 20 }
      this.loading = false
      Promise.all([
        this.getBanner(),
        this.getClass(),
        this.getHot(),
        this.getNew(),
        this.getGuessLike(),
      ]).then(() => {
        this.refresherTriggered = false
      })
    },
    // 上拉刷新更多商品
    async scrolltolower(e) {
      // console.log(e);
      if (this.loading) return
      if (this.pageParams.page < this.pages) {
        this.pageParams.page++
        this.loading = true
        await this.getGuessLike()
        this.loading = false
      }
    },
    // 猜你喜欢
    async getGuessLike() {
      const res = await getGuessLikeAPI(this.pageParams)
      // this.homeGoodsGuessLike = res.result.items
      this.homeGoodsGuessLike = [
        ...this.homeGoodsGuessLike,
        ...res.result.items,
      ]
      this.pages = res.result.pages
      this.homeGoodsGuessLikeTotal = res.result.counts
    },
    // 新鲜好物
    async getNew() {
      const res = await getNewAPI()
      this.homeNew = res.result
    },
    // 人气推荐
    async getHot() {
      const res = await getHotAPI()
      this.hotMutli = res.result
    },
    // 分类
    async getClass() {
      const res = await getClassAPI()
      this.classList = res.result
    },
    // 轮播图
    async getBanner() {
      const res = await getBannerAPI()
      this.bannerList = res.result
    },
  },

  components: { Navbar, swiper, classA, Guess },
  computed: {},
}
</script>


<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #0303dd;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
